#include "win_headers.h"
#include "main_wnd.h"

MainWnd *MainWnd::instance_ = NULL;

MainWnd *MainWnd::instance()
{
    if (instance_ == NULL)
    {
        instance_ = new MainWnd;
    }

    return instance_;
}

LRESULT CALLBACK wnd_proc(HWND h_wnd, UINT msg, WPARAM w_param, LPARAM l_param)
{
    HDC h_dc;
    PAINTSTRUCT ps;
    RECT rect;

    switch (msg)
    {
    case WM_CREATE:
        {
            return 0;
        }
    case WM_PAINT:
        {
            h_dc = BeginPaint(h_wnd, &ps);
            GetClientRect(h_wnd, &rect);

            DrawText(h_dc,
                     TEXT("hello, my deal program"), 
                     -1, 
                     &rect, 
                     DT_SINGLELINE | DT_CENTER | DT_VCENTER);

            EndPaint(h_wnd, &ps);
            return 0;
        }
    case WM_DESTROY:
        {
            PostQuitMessage(0);
            return 0;
        }
    }

    return DefWindowProc(h_wnd, msg, w_param, l_param);
}

int MainWnd::create_mainwnd(HINSTANCE h_instance, int i_cmdshow)
{
    app_name_ = TEXT("Directory Sync Tool");

    WNDCLASS wnd_class;

    wnd_class.style = CS_HREDRAW | CS_VREDRAW;
    wnd_class.lpfnWndProc = wnd_proc;
    wnd_class.cbClsExtra = 0;
    wnd_class.cbWndExtra = 0;
    wnd_class.hInstance = h_instance;
    wnd_class.hIcon = LoadIcon(NULL, IDI_APPLICATION);
    wnd_class.hCursor = LoadCursor(NULL, IDC_ARROW);
    wnd_class.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
    wnd_class.lpszMenuName = NULL;
    wnd_class.lpszClassName = app_name_;

    if (!RegisterClass(&wnd_class))
    {
        MessageBox(NULL, TEXT("This program should run on Windows NT"), app_name_, MB_ICONERROR);
        return 0;
    }

    h_wnd_ = CreateWindow(app_name_,
                          TEXT("I love Win API"),
                          WS_OVERLAPPEDWINDOW | WS_CAPTION | WS_VSCROLL| WS_HSCROLL,
                          CW_USEDEFAULT,
                          CW_USEDEFAULT,
                          CW_USEDEFAULT,
                          CW_USEDEFAULT,
                          NULL,
                          NULL,
                          h_instance,
                          NULL);    

    ShowWindow(h_wnd_, i_cmdshow);
    UpdateWindow(h_wnd_);

    MSG msg;

    while (GetMessage(&msg, NULL, 0, 0))
    {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }

    return 0;
}
